home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
wrcmdd
/
winio.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-28
|
33KB
|
967 lines
/*
WINIO.C
Stdio (e.g. printf) functionality for Windows - implementation
Dave Maxey - 1991
*/
#include <windows.h>
#include <stdlib.h>
#include <stdarg.h>
#include <malloc.h>
#include <string.h>
#include "wmhandlr.h"
#include "winio.h"
/* PROTOTYPES in alphabetic order */
void addchars(char *, unsigned);
void adjust_caret(void);
void append2buffer(char *, unsigned);
int chInput(void);
void compute_repaint(void);
int initialize_buffers(unsigned);
int initialize_class(HANDLE);
void initialize_state(void);
int initialize_window(HANDLE, HANDLE, int);
void make_avail(unsigned);
char far * nextline(char far *);
char far * prevline(char far *);
void set_font(void);
long winio_wmpaint(HWND, unsigned, WORD, LONG);
long winio_wmsize(HWND, unsigned, WORD, LONG);
long winio_wmdestroy(HWND, unsigned, WORD, LONG);
long winio_wmchar(HWND, unsigned, WORD, LONG);
long winio_wmkeydown(HWND, unsigned, WORD, LONG);
long winio_wmhscroll(HWND, unsigned, WORD, LONG);
long winio_wmvscroll(HWND, unsigned, WORD, LONG);
long winio_wmsetfocus(HWND, unsigned, WORD, LONG);
long winio_wmkillfocus(HWND, unsigned, WORD, LONG);
// this doesn't get declared in stdio.h if _WINDOWS is defined
// although it is in the Windows libraries!
int vsprintf(char *, const char *, va_list);
#define winio_caret_visible() \
((yCurrLine <= (yTopOfWin + yWinHeight)) && \
(xCurrPos <= (xLeftOfWin + xWinWidth)) && \
(xCurrPos >= xLeftOfWin))
#define CHECK_INIT() if (! tWinioVisible) return FALSE
#define MAX_X 127
#define TABSIZE 8
#define TYPE_AHEAD 256
#define WINIO_DEFAULT_BUFFER 8192
#define MIN_DISCARD 256
#define CARET_WIDTH 2
// For scrolling procedures
#define USE_PARAM 10000
#define DO_NOTHING 10001
char winio_class[15] = "winio_class";
char winio_icon[15] = "winio_icon";
char winio_title[128] = "Console I/O";
unsigned long bufsize = WINIO_DEFAULT_BUFFER;
unsigned long kbsize = TYPE_AHEAD;
int bufused, bufSOI;
int curr_font = SYSTEM_FIXED_FONT;
int tWinioVisible = FALSE;
int tCaret = FALSE, tFirstTime = TRUE;
int cxChar, cyChar, cxScroll, cyScroll, cxWidth, cyHeight;
int xWinWidth, yWinHeight, xCurrPos;
int xLeftOfWin, yTopOfWin, yCurrLine;
unsigned pchKbIn, pchKbOut;
static char far *fpBuffer, far *fpTopOfWin, far *fpCurrLine;
static char far *fpKeyboard;
static HANDLE hBuffer, hKeyboard;
HWND hWnd;
BOOL tTerminate = TRUE,
tPaint = TRUE;
DESTROY_FUNC destroy_func;
typedef struct {
int hSB, vSB;
} recVKtoSB;
/* This table defines, by scroll message, what increment to try */
/* and scroll horizontally. PGUP and PGDN entries are updated */
/* in the winio_wmsize function. */
int cScrollLR[SB_ENDSCROLL + 1] =
//UP DOWN PGUP PGDN POS TRACK TOP BOT ENDSCROLL
{ -1, +1, -1, +1, USE_PARAM, USE_PARAM, -MAX_X, MAX_X, DO_NOTHING};
/* This table defines, by scroll message, what increment to try */
/* and scroll horizontally. PGUP and PGDN entries are updated */
/* in the winio_wmsize function, and the TOP & BOTTOM entries */
/* are updated by addchar function. */
int cScrollUD[SB_ENDSCROLL + 1] =
//UP DOWN PGUP PGDN POS TRACK TOP BOT ENDSCROLL
{ -1, +1, -1, +1, USE_PARAM, USE_PARAM, -1, +1, DO_NOTHING};
/* This table associates horizontal and vertical scroll */
/* messages that should be generated by the arrow and page keys */
recVKtoSB VKtoSB[VK_DOWN - VK_PRIOR + 1] =
// VK_PRIOR VK_NEXT
{ { DO_NOTHING, SB_PAGEUP }, { DO_NOTHING, SB_PAGEDOWN },
// VK_END VK_HOME
{ SB_TOP, SB_BOTTOM }, { SB_TOP, SB_TOP },
// VK_LEFT VK_UP
{ SB_LINEUP, DO_NOTHING }, { DO_NOTHING, SB_LINEUP },
// VK_RIGHT VK_DOWN
{ SB_LINEDOWN, DO_NOTHING },{ DO_NOTHING, SB_LINEDOWN } };
/* =================================================================== */
/* the interface functions themselves..... */
/* =================================================================== */
char *gets(char *pchTmp)
{
char *pch = pchTmp;
int c;
CHECK_INIT();
bufSOI = bufused; /* mark beginning of line to limit backspace */
do {
switch (c = fgetchar())
{
case '\b' : if (pch > pchTmp) pch--; break;
case 0x1b : pch = pchTmp; break;
case EOF : bufSOI = -1; return NULL;
default : *pch = (char) c; pch++;
}
} while (c);
bufSOI = -1;
return pchTmp;
}
int printf(const char *fmt, ...)
{
va_list marker;
va_start(marker, fmt);
return vprintf(fmt, marker);
}
int vprintf(const char *fmt, va_list marker)
{
static char s[1024];
int len;
CHECK_INIT();
len = vsprintf(s, fmt, marker);
addchars(s,len);
return len;
}
int fgetchar(void)
{
CHECK_INIT();
return fputchar(chInput());
}
int kbhit(void)
{
CHECK_INIT();
return (pchKbIn == pchKbOut);
}
int fputchar(int c)
{
CHECK_INIT();
addchars(&((char) c), 1);
return c;
}
int puts(const char *s)
{
char c = '\n';
CHECK_INIT();
addchars((char *) s, strlen(s));
addchars(&c, 1);
return 0;
}
/* --------------------------------------------------------------- */
/* USED INTERNALLY - pops up an error window and returns FALSE */
/* --------------------------------------------------------------- */
int fail(char *s)
{
MessageBox(NULL,s,"ERROR",MB_OK);
return FALSE;
}
/* --------------------------------------------------------------- */
/* pops up a message window */
/* --------------------------------------------------------------- */
BOOL winio_warn(BOOL confirm, const char *fmt, ...)
{
char s[256];
va_list marker;
va_start(marker, fmt);
vsprintf(s, fmt, marker);
va_end(marker);
return (MessageBox(NULL, s, winio_title,
confirm? MB_OKCANCEL : MB_OK) == IDOK);
}
/* --------------------------------------------------------------- */
/* The application must call this function before using any of the */
/* covered stdio type calls. We need the parameter info in order */
/* to create the window. The function allocates the buffer and */
/* creates the window. It returns TRUE or FALSE. */
/* --------------------------------------------------------------- */
int winio_init(HANDLE hInstance, HANDLE hPrevInstance,
int nCmdShow, unsigned wBufSize)
{
if (tWinioVisible)
return FALSE;
if (! initialize_buffers(wBufSize))
return FALSE;
initialize_state();
if (! initialize_window(hInstance, hPrevInstance, nCmdShow))
return FALSE;
tWinioVisible = TRUE;
atexit(winio_end); /* hook into exit chain */
winio_yield();
return TRUE;
}
/* --------------------------------------------------------------- */
/* Clear the contents of the buffer.